import { defineStore } from 'pinia'
import { showToast } from 'vant'
import ContactServe from '@/api/contact'
import type { ContactStoreState } from '../type'

const useContactStore = defineStore('contact', {
  state: (): ContactStoreState => {
    return {
      visible: false,
      userId: null,
      contactUserInfo: [],
      actionsList: [],
      anomalyIndex: '' // 预警指标，发消息携带提示
    }
  },

  getters: {
    contactVisible: state => {
      return state.visible
    },
    getContactUserInfo: state => {
      return state.contactUserInfo
    }
  },

  actions: {
    /**
     * 打开弹窗
     */
    changeVisible(params: boolean) {
      this.visible = params
    },
    /**
     * 获取用户详情
     */
    async fetchUserDetail(id: any) {
      try {
        if (!id) {
          showToast('暂无联系人')
          return
        }
        if (Array.isArray(id) && id.length > 0) {
          // 多个联系人，只显示视频
          this.actionsList = [{ name: '视频会议' }]
          this.changeVisible(true)
          return
        }
        const resp = await ContactServe.userDetail({ id: id })
        this.contactUserInfo = resp.data

        this.actionsList = [
          {
            name: '发消息'
          },
          {
            name: '打电话'
          },
          {
            name: '视频会议'
          }
        ]
        this.changeVisible(true)
      } catch (error) {}
    }
  }
})

export { useContactStore }
